package tgc.edu.ljc.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@Entity
public class Student {
	private Integer id;
	private String name;
	private String stuNum;
	private String sex;
	private TbClass tbClass;
	private String password="1111";
	private List<Course> courses=new ArrayList<>();
	private List<Grade> grades=new ArrayList<>();
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=10)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(length=10)
	public String getStuNum() {
		return stuNum;
	}
	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}
	@Column(length=2)
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@ManyToOne
	public TbClass getTbClass() {
		return tbClass;
	}
	public void setTbClass(TbClass tbClass) {
		this.tbClass = tbClass;
	}
	public Student() {
		super();
	}
	
	public Student(Integer id, String name, String stuNum, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.stuNum = stuNum;
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "id:"+id+",name:"+name+",stuNum:"+stuNum+",sex:"+sex+",class:"+tbClass.getName();
	}
	
	@OneToMany(mappedBy="student")
	public List<Grade> getGrades() {
		return grades;
	}
	public void setGrades(List<Grade> grades) {
		this.grades = grades;
	}
	public Student(Integer id) {
		super();
		this.id = id;
	}
	/*@ManyToMany
	@JoinTable(name="student_course",joinColumns= {@JoinColumn(name="sid")},inverseJoinColumns= {@JoinColumn(name="cid")})
	public List<Course> getCourses() {
		return courses;
	}
	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}*/
	
	@Transient//非持久化
	public List<Course> getCourses() {
		List<Course> courses=new ArrayList<>();
		for (Grade grade : getGrades()) {
			courses.add(grade.getCourse());
		}
		return courses;
	}
	
	@Transient
	public String getCourseNames() {
		String str="";
		for (Course course : getCourses()) {
			str+=course.getName()+",";
		}
		if(str.length()>0) {
			str=str.substring(0,str.length()-1);
		}
		return str;
	}
	
	@Column(length=20)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
	
}
